package com.qf.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.JwtUtils;
import com.qf.common.PageParam;
import com.qf.common.PageVO;
import com.qf.common.UserUtils;
import com.qf.entity.User;
import com.qf.exception.CustomerException;
import com.qf.mapper.UserMapper;
import com.qf.param.LoginParam;
import com.qf.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zdl
 * @date 2025/4/14
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public String loginUser(LoginParam loginParam) {
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getUsername, loginParam.getUsername()));
        if (user == null){
            throw new CustomerException("500","用户名错误");
        }
        String md5Pwd = SecureUtil.md5(loginParam.getPassword());
        if (!(user.getPassword().equals(md5Pwd))) {
            throw new CustomerException("500","密码错误");
        }
        //返回token  使用jwt作为token的生成方式
        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        return JwtUtils.createJwt(map);
    }

  /*  @Override
    public PageVO<User> getUserList(PageParam pageParam) {
        Integer uid = UserUtils.getUser();
        Page<User> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        Page<User> userPage = userMapper.selectPage(page, Wrappers.lambdaQuery(User.class).eq(User::getId, uid));

        List<User> records = userPage.getRecords();
        Calendar calendar = Calendar.getInstance();
        for (User record : records) {
            if (record.getAm() == null || record.getPm() == null) {
                record.setStatus("未打卡");
                continue;
            }
            calendar.setTime(record.getAm());
            // 设置标准上班时间（9:00）
            calendar.set(Calendar.HOUR_OF_DAY, 9);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date standardAm = calendar.getTime();

            calendar.setTime(record.getPm());
            // 设置标准下班时间（18:00）
            calendar.set(Calendar.HOUR_OF_DAY, 18);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date standardPm = calendar.getTime();

            // 比较实际打卡时间与标准时间
            boolean amOk = record.getAm().before(standardAm) || record.getAm().equals(standardAm);
            boolean pmOk = record.getPm().after(standardPm) || record.getPm().equals(standardPm);

            if (amOk && pmOk) {
                record.setStatus("正常");
            } else if (!amOk && pmOk) {
                record.setStatus("迟到");
            } else if (amOk && !pmOk) {
                record.setStatus("早退");
            } else {
                record.setStatus("迟到且早退");
            }

        }
        PageVO<User> pageVO = new PageVO<>();
        pageVO.setTotal(userPage.getTotal());
        pageVO.setDataList(records);
        return  pageVO;
    }*/

    @Override
    public User getUserById() {
        Integer uid = UserUtils.getUser();
        return userMapper.selectById(uid);
    }
}
